package org.asiainfo.flowable.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.validation.constraints.NotEmpty;
import lombok.RequiredArgsConstructor;
import org.asiainfo.common.core.domain.R;
import org.asiainfo.common.core.validate.AddGroup;
import org.asiainfo.common.core.validate.EditGroup;
import org.asiainfo.common.log.annotation.Log;
import org.asiainfo.common.log.enums.BusinessType;
import org.asiainfo.common.mybatis.stream.core.page.PageQuery;
import org.asiainfo.common.mybatis.stream.core.page.TableDataInfo;
import org.asiainfo.common.web.core.BaseController;
import org.asiainfo.flowable.domain.bo.WfModelBo;
import org.asiainfo.flowable.domain.vo.WfModelVo;
import org.asiainfo.flowable.service.WfModelService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 流程模型管理
 *
 * @author dotor-ww
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/wfModel")
@Validated
public class WfModelController extends BaseController {

    private final WfModelService wfModelService;

    /**
     * 分页查询流程模型
     *
     * @param wfModelBo 流程模型对象
     * @param pageQuery 分页
     * @return 流程模型
     */
    @SaCheckPermission("system:model:list")
    @GetMapping("/list")
    public TableDataInfo<WfModelVo> list(WfModelBo wfModelBo, PageQuery pageQuery) {
        return wfModelService.queryPageList(wfModelBo, pageQuery);
    }

    /**
     * 分页查询流程模型历史
     *
     * @param wfModelBo 流程模型对象
     * @param pageQuery 分页
     * @return 历史流程模型
     */
    @GetMapping("/historyList")
    public TableDataInfo<WfModelVo> historyList(WfModelBo wfModelBo, PageQuery pageQuery) {
        return wfModelService.historyList(wfModelBo, pageQuery);
    }

    /**
     * 查询流程模型详细信息
     *
     * @param modelId 流程模型ID
     * @return 流程模型
     */
    @SaCheckPermission("system:model:query")
    @GetMapping("/{modelId}")
    public R<WfModelVo> getInfo(@NotEmpty(message = "模型ID不能为空") @PathVariable String modelId) {
        return R.ok(wfModelService.queryById(modelId));
    }

    /**
     * 获取流程模型表单数据
     *
     * @param modelId 流程模型ID
     * @return 流程模型表单数据
     */
    @SaCheckPermission("system:model:query")
    @GetMapping("/bpmnXml/{modelId}")
    public R<WfModelVo> getBpmnXml(@NotEmpty(message = "模型ID不能为空") @PathVariable String modelId) {
        return R.ok(wfModelService.queryBpmnXmlById(modelId));
    }

    /**
     * 新增流程模型
     *
     * @param wfModelBo 流程模型对象
     * @return 新增结果
     */
    @SaCheckPermission("system:model:add")
    @Log(title = "流程模型管理", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WfModelBo wfModelBo) {
        wfModelService.insertByBo(wfModelBo);
        return R.ok();
    }

    /**
     * 修改流程模型
     *
     * @param wfModelBo 流程模型对象
     * @return 修改结果
     */
    @SaCheckPermission("system:model:edit")
    @Log(title = "流程模型管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WfModelBo wfModelBo) {
        wfModelService.updateByBo(wfModelBo);
        return R.ok();
    }

    /**
     * 保存流程模型
     *
     * @param wfModelBo 流程模型对象
     * @return 保存结果
     */
    @SaCheckPermission("system:model:add")
    @Log(title = "流程模型管理", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    public R<Void> save(@RequestBody WfModelBo wfModelBo) {
        wfModelService.save(wfModelBo);
        return R.ok();
    }

    /**
     * 删除流程模型
     *
     * @param modelIds 流程模型ID集合
     * @return 删除结果
     */
    @SaCheckPermission("system:model:remove")
    @Log(title = "流程模型管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{modelIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable String[] modelIds) {
        wfModelService.deleteWithValidByIds(List.of(modelIds));
        return R.ok();
    }

    /**
     * 设置流程模型为最新
     *
     * @param modelId 流程模型ID
     * @return 设置结果
     */
    @SaCheckPermission("system:model:edit")
    @Log(title = "流程模型管理", businessType = BusinessType.UPDATE)
    @PostMapping("/latest")
    public R<Void> latest(@RequestParam String modelId) {
        wfModelService.latestModel(modelId);
        return R.ok();
    }

    /**
     * 部署流程模型
     *
     * @param modelId 流程模型ID
     * @return 部署结果
     */
    @SaCheckPermission("system:model:edit")
    @Log(title = "流程模型管理", businessType = BusinessType.UPDATE)
    @PostMapping("/deployModel")
    public R<Void> deployModel(@RequestParam String modelId) {
        return toAjax(wfModelService.deployModel(modelId));
    }
}
